Page({
  data: {
    order: null,
    target: 0,
    targetItems: [
      {name: '订单', value: '0', checked: true},
      {name: '商品', value: '1'}
    ],
    type: 0,
    typeItems: [
      {name: '退款(无需退货)', value: '1', checked: true},
      {name: '退货退款', value: '2'},
      //{name: '我要换货', value: '3'},
    ],
    /*logisticsStatus:0,
    logisticsStatusItems: [
      {name: '未收到货', value: '0', checked: true},
      {name: '已收到货', value: '1'}
    ],*/
    reasonIndex: -1,
    reasons: [
      '不喜欢/不想要', 
      '空包裹', 
      '未按约定时间发货',
      '快递/物流一直未送达',
      '货物破损已拒签',
      '退运费',
      '规格尺寸与商品页面描述不符',
      '功能/效果不符',
      '质量问题',
      '少件/漏发',
      '包装/商品破损',
      '发票问题',
    ],
    uploadUrl: wx.util.interfaceUrl('common.upload.images')['url'],
    imgLimit: 9,
    imgList: []
  },
  onLoad: function(options) {
    wx.util.request('order.detail', {
      query: `id=${options.id}`
    }).then(order => {
      this.setData({
        order: order
      });
    });
  },
  onPullDownRefresh: function () {
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 300);
  },
  targetItemsChange: function (e) {
    let targetItems = this.data.targetItems;
    for (var i in targetItems) {
      targetItems[i].checked = (targetItems[i].value == e.detail.value);
    }
    this.setData({
      target: e.detail.value,
      targetItems: targetItems
    });
  },
  chooseProduct: function (e) {
    let product = this.data.order.product,
        selected = e.detail.value;
    for (let i in product) {
      product[i].checked = false;
      for (let j in selected) {
        if (product[i].id == selected[j]) {
          product[i].checked = true;
        }
      }
    }
    this.setData({
      [`order.product`]: product
    });
  },
  dropDownList: function () {
    this.setData({
      dropdownShow: !this.data.dropdownShow
    });
  },
  reasonChange: function (e) {
    this.setData({
      reasonIndex: e.currentTarget.dataset.index,
      dropdownShow: false
    });
  },
  imgUpload: function (e) {
    if (e.detail.status === 1) {
      this.setData({
        imgList: e.detail.imgArr
      });
    }
  },
  apply: async function () {
    let ids = [], applyRes = [], api = 'refundApply',
        d = this.data, product = d.order.product,
        evidence = JSON.stringify(d.imgList);
    if (d.reasonIndex < 0) {
      wx.util.toast('申请售后的原因不能为空');
      return;
    }
    // 根据用户选择，获取要提交申请的商品id
    for (let pro of product) {
      // 整单
      if (d.target == 0) {
        ids.push(pro.id);
        continue;
      }
      // 指定商品
      if (pro.checked) ids.push(pro.id);
    }
    // 根据订单状态，判断调用的接口
    if (d.order.order_status == 4) {
      api = 'apply';
    }
    // 目前未能整单申请售后，只能按商品逐一申请
    for (let id of ids) {
      let res = await wx.util.request(`order.${api}`, {
        body: {
          id: id,
          refund_type: d.typeItems[d.type].value,
          refund_reason: d.reasons[d.reasonIndex],
          refund_evidence: evidence
        }
      });
      applyRes.push(res);
    }
    // 检查提交未成功的商品
    let title, content, failNum = ids.length - applyRes.length;
    if (failNum > 0) {
      title = '提示';
      content = `共有${failNum}件商品提交未成功，请稍后重试`;
    } else {
      title = '成功';
      content = '请耐心等待';
    }
    wx.showModal({
      title: title,
      content: content,
      showCancel: false,
      confirmText: '我知道了',
      success() {
        if (failNum == 0) {
          wx.navigateBack();
        }
      }
    });
  }
})